

import re
import sys
from TableTwo import  Tabletwo
import numpy as np

class Texttwo():
    def __init__(self,**params1):
        self.params1 = params1
        self.config = params1['config']
        self.get_money = params1['get_money']
        self.clear_solo = params1['clear_solo']
    def pub_split(self,html):
        end_html = {}
        #特殊标签清洗
        restr = [
                ['◄\d((投标人?|供应商)的?资[格质](?:要求|条件))','|','◄(?!<[.﹑、])\d([^.、﹑]{0,20}?)◄','◄1、\\1◄'],
                ['◄(（[一二三四五六七八九十]）)?((投标人?|供应商)的?资[格质](?:要求|条件))','一[、◄.．:﹑：](?!二)','◄((?:（[一二三四五六七八九十]）)?(?:投标人?|供应商)资[格质](?:要求|条件))','◄一、\\1'],
                ['◄((投标人?|供应商)的?资[格质](?:要求|条件))', '(?<![、.．\d#])[1-5]{1,3}[、.．\s](工程概况|招标文件的获取|投标文件的递交|发布公告的媒介|联系方式|招标条件|招标编号)', '◄((?:投标人|供应商)资[格质](要求|条件))', '◄1、\\1'],
                ['◄\d[、◄.．﹑：:]\d?[、◄.．：﹑:]?((投标人?|供应商)的?资[格质](?:要求|条件))', '(?<=◄)一[、◄.．:﹑：](?!二)', '◄\d[、◄.．﹑：:]\d?[、◄.．﹑：:]?((投标人|供应商)的?资[格质](要求|条件))', '◄一、\\1'],
                ['\d{1,3}[、.．\s]报名条件','[一二三四五六七八九十\d][、◄.．﹑：:]','\d{1,3}[、.﹑．\s]报名条件','一、报名条件'],
                ['◄((投标人?|供应商)的?资[格质](?:要求|条件)).{0,10}◄','|','(◄((投标人?|供应商)的?资[格质](?:要求|条件)).{0,10}◄)','\\1:']
                ]
        for i in restr:
            if re.search(i[0],html) and re.search(i[1],html):
                html = re.sub(i[2],i[3],html)
        restr = [
                ['(?<![一二三四五六七八九十\d][、◄.．﹑：:])((投标人|供应商)的?资[格质](?:要求|条件)|供应商资格条件审查|具备下列条件)',
                 '◄(招标文件的获取|评标方法|招标文件发售时间|招标文件发售地点|.{1,5}供应商符合性审查|有意参与者|时间安排|采购文件澄清|澄清及质疑事项|报名时需携带|招标文件购买)','\d[.、）]',
                 '(?<![一二三四五六七八九十\d][、◄.．﹑：:]◄)(?:(投标人|供应商)的?资[格质](?:要求|条件)|供应商资格条件审查|具备下列条件)']
                ]
        for i in restr:
            obj1 = re.search(i[0],html)
            obj11 = re.search(i[3],html)
            obj2 = re.search(i[2],html)
            if obj1 and obj2 and obj11:
                if re.search(i[2],html[obj1.end():obj1.end() + 10]) and obj1.start() != obj2.start():
                    obj = re.search(f'{i[0]}.*({i[1]})',html)
                    if obj:
                        use = obj.group()
                        use = re.sub('\d[、.）]\d?(?<![万元%])|◄','',use)
                        tt = obj.group(3)[1:]
                        use = re.sub(f'({tt})','◄\\1',use)
                        tt = html[:obj.start()]
                        if re.search('(?<![一二三四五六七八九十\d][、◄.．﹑：:])((投标人|供应商)的?资[格质]要求|供应商资格条件审查|具备下列条件):',use) == None:
                            use = re.sub('(?<![一二三四五六七八九十\d][、◄.．﹑：:])((投标人|供应商)的?资[格质]要求|供应商资格条件审查|具备下列条件)','\\1:',use)
                        html = tt + use + html[obj.end():]
        #标题管理
        level = [
                [{'(?<=◄)一[、◄.．﹑:：](?![二.])':['二[、◄.．﹑:：](?!三)|三[、◄.．﹑:：](?!四)|七[、◄.．﹑:：](?!八)','[一二三四五六七八九十]+[、．.﹑◄:：](?![一二三四五六七八九十])']},
                {'(?<=◄)二[、◄.．﹑:：](?![三.])': ['三[、◄.﹑．:：](?!四)', '[一二三四五六七八九十]+[、．﹑.◄：:]']}],
                [{'(?<![、.﹑．\d#])[1-5]{1,3}[、.．\s](?![\da-zA-Z])':['|','(?<![、.﹑．\d])[0-9２３]{1,3}[、﹑.．\s](?![\da-zA-Z])']},
                {'(?<=◄)（[一二三四五六七八九十]）':['|','（[一二三四五六七八九十]+）[、﹑.．◄]?']}],
                [{'(?<![、﹑.．\d])[1-5]{1,3}[、.﹑．\s][1-9]{1,3}(?![\da-zA-Z])':['|','(?<![、.﹑．\d])[0-9]{1,3}[、﹑.．\s][1-9]{1,3}(?![\da-zA-Z、.﹑．])']}],
                ]
        start_me = 9999999
        for split_key_ls in level:
            for split_key_dict in split_key_ls:
                key = list(split_key_dict.keys())[0]
                obj1 = re.search(key, html)
                obj2 = re.search(split_key_dict[key][0], html)
                if obj1 and obj2:

                    start = obj1.start()
                    if start < start_me:
                        start_me = start
                        use_split_key = split_key_dict
            if start_me == 9999999:
                continue
            else:
                if re.search('(?<=◄)[一二三四五六七八九十]+[、．.◄:：]((投标人|供应商)资[格质]要求)',html):
                    use_split_key = level[0][0]
                key = list(use_split_key.keys())[0]
                html = re.split(use_split_key[key][1], html)
                end_html['first'] = html[0] + '◄'
                for i in html[1:]:
                    if re.search('^[.◄:\d、]{1,5}', i):
                        i = i.split(re.search('^[.◄:\d、]{1,5}', i).group(),1)[1]
                    index_ls = ['◄', ':','：', '1、', '1.','2、','2.','3、', '3.','4.','4、', '5.','5、', '6.','6、', '7.','7、', '8.','8、', '9.','9、']
                    split_key = ''
                    split_index = 99999999999999
                    for indexi in index_ls:
                        if indexi in i:
                            index1 = i.index(indexi)
                            if index1 < split_index:
                                split_index = index1
                                split_key = indexi
                    if re.search('[1-9][、．.]|[◄:：]',i) == None or split_key == '':
                        continue
                    title, value = i.split(split_key, 1)
                    value = re.sub('(\d+)[年/-](\d+)◄[月/-]◄(\d+)日', r'\1年\2月\3日', value)
                    end_html[title] = value + '◄'
                break
        if start_me == 9999999:
            end_html['no_region'] = html
        return end_html
    def get_all_data(self,end_html):
        end_dict = {}
        for i in end_html:
            all_key_ls = []
            res_ls = self.config['res_ls']
            res_key = False
            for res_lsi in res_ls:
                if (re.search(res_lsi[0],i) and re.search(res_lsi[1],i) == None):
                    res_key = True
                    break
            if res_key == True or (re.search('[:为]',end_html[i]) == None):
                end_dict[i] = (end_html[i],'')
            else:
                if re.search('[:为]', end_html[i][:30]):
                    if re.search('[^1-9◄]',end_html[i][0]):
                        tt = ''
                        for _ in end_html[i]:
                            if _ == ':':
                                break
                            tt += _
                        if re.search('◄$',tt):
                            tt = tt[:-1]
                        all_key_ls.append(tt)
                if i != 'first' and ':' not in end_html[i].split('◄',1)[0]:
                    end_dict[i] = (end_html[i].split('◄',1)[0],'')
                if ':' in end_html[i].split('◄',1)[0]:
                    all_key_ls.append(end_html[i].split('◄',1)[0].split(':')[0])
                end_html[i] = end_html[i].replace('◄:',':')
                if re.search('(([0-9]{1,2}[、.\s．）])+[0-9]?|◄)',end_html[i]):
                    obj = re.finditer('(([0-9]{1,2}[、.\s．）])+[0-9]?|[◄,。;])([？/（）□\d\w、.,\s]*?):',end_html[i])
                    for j in obj:
                        all_key_ls.append(j.group())
                else:
                    if ':' not in end_html[i]:
                        continue

            all_key_ls = list(np.unique(all_key_ls))
            end_tag = ''
            for all_key_lsi in all_key_ls:
                end_tag += all_key_lsi + '|'
            end_tag = end_tag + '◄'
            for j in all_key_ls:
                key = re.sub('^(([0-9]+[、.\s．）]){1,2}[0-9]?|[◄,。;]){1,}','',j)
                obj = re.finditer(f'(?<={re.escape(key)}):?◄?(.*?)(?={re.escape(end_tag)})',end_html[i])
                ll = []
                for obji in obj:
                    start = obji.start() - 200 if obji.start() >= 200 else 0
                    before_data = end_html[i][start:obji.start()]
                    before_num = -1
                    before_dd = ''
                    obj = re.finditer("(?<!集中)(业主|比[优选]|询价|询比|招标(（(采购|建设)）)?|建设|主办|[出招]租|出让|原?采购|发包|合同甲|谈判)(人|机构|单位|部门|方)(?!式)",before_data)
                    obj = list(obj)
                    if obj != []:
                        obj = obj[-1]
                        if obj.start() > before_num:
                            before_dd = '业主'
                            before_num = obj.start()
                    obj = re.finditer("(招标|交易|采购)(（采购）)?代理(机构|公司|单位|系统|名称|人)|(招标代理|代理机构(（.*?）)?|填报单位)(名称)?:?|自主招标执行部门|集中采购机构",before_data)
                    obj = list(obj)
                    if obj != []:
                        obj = obj[-1]
                        if obj.start() > before_num:
                            before_dd = '招标代理单位'
                            before_num = obj.start()

                    obj = re.finditer("项目联系",before_data)
                    obj = list(obj)
                    if obj != []:
                        obj = obj[-1]
                        if obj.start() > before_num:
                            before_dd = '业主'
                            before_num = obj.start()
                    ttt = re.sub('^(([0-9]{1,2}[、.\s．])+[0-9]?|[◄:]){1,}(?!\d{0,}(万?元|%))','',obji.group())
                    if re.search('要求|[需须|内容]',key) == None:
                        ttt = ttt.split('。')[0]
                    ll.append((ttt,before_dd))
                end_dict[key] = end_dict.get(key,[]) + ll if not isinstance(end_dict.get(key,[]),(str,tuple)) else [end_dict.get(key)] + ll
        return end_dict
    def get_winner_company_name(self, value):
        if value[1] == "为":
            value = value[1:]
        if value[-1] == "为":
            value = value[0:-1]
        return value

    def clear_html(self,html):
        re_ls = self.config['re_ls']
        html = html.replace('?','？')
        html = html.replace('(', '（')
        html = html.replace(')', '）')
        for i in re_ls:
            try:
                html = re.sub(i[0], i[1], html)
            except Exception as e:
                print('re wrong..................')
                continue

        return html
    def get_data(self,html):
        r = {}
        for i in self.config['find_key_two']:
            obj = re.finditer(i["value"], html, re.I | re.S)
            for x in obj:
                r[x.start()] = {"key": i["key"], "value": x.group()}
        end_data = []
        end_list = {}
        winner_index = 0
        tender = ""
        rx = sorted(r.items(), key=lambda d: d[0], reverse=False)
        for i in range(0, len(rx)):
            v = rx[i][1]
            vkey = v["key"]
            vreg = v["value"]
            need_key = [_['key'] for _ in self.config['find_key_two']]
            if vkey in need_key:
                # if vkey not in ['业主','招标代理单位']:
                if vkey not in end_list:
                    if vreg in ('◄', ':◄'):
                        continue
                    end_list[vkey] = vreg
        if end_list != {}:
            end_data.append(end_list)
        return end_data
    def change_and_in(self,all_data_dict):
        end_dict = {}
        need_key = list(self.config['find_key_two'].keys())
        for i in all_data_dict:
            for j in self.config['replace_keyword_two']:
                if (len(i) <= 30 or (re.search('资格要求|具备(以下|下列)条件',i) != None and re.search('[《（]|供应商参加',i) != None)) and  re.search(j[0],i) != None and re.search(j[2],i) == None:
                    change = j[1]
                else:
                    change = i
                change = change.replace(':','')

                if change in need_key:
                    value = all_data_dict[i]
                    value_ls = []
                    #获取values
                    if not isinstance(value,list):
                        if re.search(self.config['find_key_two'][change][0],value[0]) and re.search(self.config['find_key_two'][change][1],value[0]) == None:
                            if value[0].split(':')[0] in need_key and change != '投标要求':
                                continue
                            value_ls = [value]
                        else:
                            continue
                    else:
                        for _ in value:
                            if _ not in value_ls and _[0].split(':')[0] not in need_key:
                                if re.search(self.config['find_key_two'][change][0],_[0]) and re.search(self.config['find_key_two'][change][1],_[0]) == None:
                                    value_ls.append(_)
                        if value_ls == []:
                            continue
                    #value存入最终数据

                    if isinstance(value_ls,list):
                        for value_lsi in value_ls:
                            if re.search('^[1-9]?-?1[、.][1-9]?$',value_lsi[0]) or value_lsi[0] == '':
                                value_ls.remove(value_lsi)
                    else:
                        if re.search('^[1-9]?-?1[、.][1-9]?$', value_ls[0]) or value_ls[0] == '':
                            continue
                    if value_ls == []:
                        continue

                    if change in end_dict:
                        end_dict[change] = end_dict[change] + value_ls
                    else:
                        end_dict[change] = value_ls
                    # end_dict['original_'+change] = i
                    break

        return end_dict
    def end_data_clear(self,end_dict):
        end_dict_ = end_dict.copy()
        judge_ls = list(end_dict.keys())
        if '名称' in judge_ls:
            judge_ls.remove('名称')
            judge_ls = ['名称'] + judge_ls

        for i in judge_ls:
            if re.search('^联系人$|^地址$|^联系电话$|^传真$|^名称$', i) and 'original' not in i:
                for cmi in end_dict[i]:
                    if cmi[1] != '' and cmi[1] + i not in end_dict_:
                        end_dict_[cmi[1] + i] = cmi[0].replace('♥','.')
                del end_dict_[i]
            else:
                if not isinstance(end_dict[i],list):
                    value = re.sub('◄|(?!\d):(?!\d)','',end_dict[i][0]).replace('♥','.')
                else:
                    if end_dict[i] == []:
                        continue
                    value = re.sub('◄|(?!\d):(?!\d)','',end_dict[i][0][0]).replace('♥','.')
                end_dict_[i] = value
        end_dict = end_dict_
        end_dict = Tabletwo(**self.params1).kaibregion(end_dict)
        if '获取招标文件' in end_dict:
            key = False
            ls = [  ['期限|日期','时间'],
                    ['方式|获取时限、获取地点','方法'],
                    ['价格|购买费','售价'],
                    ['地址','地点']]
            for lsi in ls:
                end_dict['获取招标文件'] = re.sub(lsi[0],lsi[1],end_dict['获取招标文件'])
            ls = ['时间','地点','方法','售价']
            index = 0
            rm = '。|◄'
            last = ''

            for lsi in ls:
                if lsi not in end_dict['获取招标文件']:
                    continue
                indexi = end_dict['获取招标文件'].index(lsi)
                if indexi >= index:
                    index = indexi
                    rm += last + '|'
                    last = lsi
                else:
                    rm += lsi + '|'
            rm += last
            for lsi in ls:
                if lsi not in end_dict['获取招标文件']:
                    continue
                if lsi == last:
                    robj = re.search(f'(?<={lsi}):?(.*)', end_dict['获取招标文件'])
                    if robj:
                        if '招标文件获取的' + lsi not in end_dict:
                            tt = robj.group()
                            if tt == '':
                                continue
                            if tt[0] == ':':
                                tt = tt[1:]
                            end_dict['招标文件获取的' + lsi] = tt
                        key = True
                else:
                    rm_t = re.sub('\|'+lsi,'',rm)
                    tobj = re.search(f'(?<={lsi}):?(.*?)(?={rm_t})',end_dict['获取招标文件'])
                    if tobj:
                        if '招标文件获取的' + lsi not in end_dict:
                            tt = tobj.group()
                            if tt != '':
                                if tt[0] == ':':
                                    tt = tt[1:]
                                end_dict['招标文件获取的' + lsi] = tt
                            key = True
            if key == True:
                del end_dict['获取招标文件']
        if '报名截止日期' in end_dict:
            end_dict['报名截止日期'] = re.split('地点|,|递交地点|（?北京时间）',end_dict['报名截止日期'])[0]
        if '招标公告发布日期' in end_dict:
            end_dict['招标公告发布日期'] = re.split('阅读次数|来源',end_dict['招标公告发布日期'])[0]
        change_date = ['报名截止日期','招标公告发布日期','工期','招标文件获取的时间']
        for change_datei in change_date:
            if change_datei not in end_dict:
                continue
            end_dict[change_datei] = re.sub('(\d+)[/-](\d+)[/-]([0123][0-9]|[1-9])',r'\1年\2月\3日',end_dict[change_datei])
            obj = re.compile('(\d+)年(\d+)月(\d+)日(\d{2})\d+')
            def change(m):
                shi = m.group(4) if int(m.group(4)) <= 24 else m.group(4)[0]
                return f'{m.group(1)}年{m.group(2)}月{m.group(3)}日{shi}时'
            end_dict[change_datei] = obj.sub(change,end_dict[change_datei])
            end_dict[change_datei] = re.split('[）（]',end_dict[change_datei])[0]
        end_dict = Tabletwo(**self.params1).province_city(end_dict)
        end_dict = Tabletwo(**self.params1).communicate_number_clear(end_dict)
        end_dict = Tabletwo(**self.params1).especial_handle(end_dict)
        end_dict = Tabletwo(**self.params1).cumunicate(end_dict)
        end_dict = Tabletwo(**self.params1).hezhun(end_dict)
        end_dict = Tabletwo(**self.params1).meijie(end_dict)



        company = ['业主','招标代理单位']
        for companyi in company:
            if (companyi+'联系电话') in end_dict and re.search('\d{3,4}-\d{9,}',end_dict[companyi+'联系电话']):

                qunumber,number = end_dict[companyi+'联系电话'].split('-',1)
                if len(qunumber) == 3:
                    end_dict[companyi + '联系电话'] = qunumber + '-' + number[:8]
                else:
                    end_dict[companyi + '联系电话'] = qunumber + '-' + number[:7]

        if '建设规模_fir' in end_dict:
                end_dict['建设规模'] = end_dict['建设规模_fir']
                del end_dict['建设规模_fir']
        if '项目地点城市' in end_dict:
            if '项目位于' in end_dict['项目地点城市']:
                end_dict['项目地点城市'] = end_dict['项目地点城市'].split('项目位于')[1]

        m_ls = ['投资额','预算金额','最高限价','代理服务收费标准及金额']
        for m_lsi in m_ls:
            if m_lsi in end_dict:
                end_dict[m_lsi] = end_dict[m_lsi].replace('♥','.')
                dd = re.findall(r'([.♥,拾佰仟萬亿圆元万整%\d]{1,20}|[零壹贰貳叁肆伍陆柒捌玖拾佰仟萬亿圆元万整]{3,})', end_dict[m_lsi])
                if dd != [] and  re.search('[拾佰仟萬亿圆元万整\d零壹贰貳叁肆伍陆柒捌玖拾佰仟萬亿圆元万整]',end_dict[m_lsi]):
                    end_dict[m_lsi] = self.get_money(dd[0])
                else:
                    end_dict[m_lsi] = ''
        if '投标要求' in end_dict:
            dd = re.findall(r'是否接受联合体(.)', end_dict['投标要求'])
            if dd != []:
                if '是否接受联合体' not in end_dict:
                    end_dict['是否接受联合体'] = dd[0]
            else:
                end_dict['投标要求'] = end_dict['投标要求']
            obj = re.search('(?<!\d)\d[.．、]\d?(?!\d?[万元%])(?!\d{2,})|（?(?:[一二三四五六七八九十]|\d{1,2}(?!\d?[万元%]))）|[①②③④⑤]',end_dict['投标要求'])
            if obj:
                ls = re.split('(?<!\d)\d[.．、]\d?(?!\d?[万元%])(?!\d{2,})|（?(?:[一二三四五六七八九十]|\d{1,2}(?!\d?[万元%]))）|[①②③④⑤]',end_dict['投标要求'])
                for i in ls:
                    if isinstance(i,str):
                        if i in ['','1','一','.'] or re.search('^[一二三四五六七八九十]$|^\d[.、]?\d?$',i):
                            ls.remove(i)
                end_dict['投标要求'] = ls
            else:
                end_dict['投标要求'] = [end_dict['投标要求']]
        return end_dict
    def value_clear(self,end_dict):
        if '报名截止日期' in end_dict:
            if '开标时间' in end_dict['报名截止日期']:
                end_dict['报名截止日期'] = end_dict['报名截止日期'].split()
    def text_part(self,html):

        html = self.clear_solo(html,self.config['dispose_solo_text_dict'],self.params1['data_source_id'])
        html = self.clear_html(html)
        region_html = self.pub_split(html)
        all_data_dict = self.get_all_data(region_html)
        end_data = self.change_and_in(all_data_dict)
        return end_data
    def text_all(self,html):
        text_all_data = {}
        for i in self.config['replace_keyword_two']:
            html = re.sub(i[0],i[1],html,re.I|re.M)
        for i in self.config['find_key_two']:
            obj = re.finditer(f"(?<={re.escape(i)})[:◄]{0,6}(.*?)◄",html)
            if obj:
                for x in obj:
                    value = x.group()
                    text_all_data[i] = text_all_data.get(i,[]) + [value]
        for i in text_all_data:
            while '◄' in text_all_data[i]:
                text_all_data[i].remove('◄')
        return text_all_data
    def supply(self,text_part_data):
        need_ls = ['项目名称', '标段','标的信息','业主名称','业主联系人', '业主联系电话', '业主地址', '业主传真',
                   '招标代理单位名称', '招标代理单位联系人', '招标代理单位联系电话', '招标代理单位传真', '招标代理单位地址',
                   '工期', '项目类型', '项目地点', '项目地点城市', '项目地点省份', '建设规模', '招标公告发布日期', '报名截止日期',
                   '投标要求', '发布公告的媒介', '评标成员','招标文件获取的时间','招标文件获取的地点','招标文件获取的方法','招标文件获取的售价','获取招标文件','资金来源',
                   '开标地点','标段划分','招标范围','投资额','采购需求','是否接受联合体','开标地点','发布公告的媒介_来源链接','开标时间','核准文号','采购方式','最高限价',
                   '预算金额','代理服务收费标准及金额','项目编号','核准单位','合同履行期限']
        for i in need_ls:
            if i not in text_part_data:
                text_part_data[i] = '' if i != '投标要求' else []
        return text_part_data
    def part_two_text(self,html):
        text_part_data = self.text_part(html)
        text_all_data = self.text_all(html)
        text_part_data = self.end_data_clear(text_part_data)
        text_part_data = self.supply(text_part_data)

        return text_part_data


